package com.anolesoft.amljmes.billing.controller;

import com.alibaba.fastjson.JSONObject;
import com.anolesoft.amljmes.billing.dto.CategoryInfoDto;
import com.anolesoft.amljmes.billing.service.CategoryInfoService;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.security.util.SecurityUtils;
import com.anolesoft.yr.demo.BaseQueryParamVO;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/jcwh/category")
public class CategoryInfoController {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    @ToString(callSuper = true)
    @EqualsAndHashCode(callSuper=true)
    public static class DemoVO extends BaseQueryParamVO implements Serializable {
        private String categorynName;
        private String factoryId;
    }

    @Autowired
    public CategoryInfoService categoryInfoService;

    //查询
    @PostMapping(value="/query",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryCategoryInfo(DemoVO demoVO){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        demoVO.setFactoryId(loginUser.getFacCode());

        List<CategoryInfoDto> allList = categoryInfoService.queryCategoryInfo(demoVO);
        System.out.println(">>>>>>"+allList.toString());
        int pn =  demoVO.getPageNo().intValue();
        int ps = demoVO.getPageSize().intValue();
        int endIndex = pn * ps > allList.size() ? allList.size() : pn*ps;
        List<CategoryInfoDto> list = allList.subList( (pn-1) *ps, endIndex  );
        PageResult pageResult = new PageResult(list, demoVO.getPageSize(), demoVO.getPageNo(), allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }
    //新增
    @PostMapping(value="/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult addCategory(@RequestBody CategoryInfoDto queryInfoDto){
        System.out.println(">>>>>>add");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryInfoDto.setCjr(loginUser.getEmpUid());
        queryInfoDto.setFactoryId(loginUser.getFacCode());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,categoryInfoService.addCategory(queryInfoDto));
    }
    //修改
    @PostMapping(value="/update", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult updateCategory(@RequestBody CategoryInfoDto queryInfoDto){
        System.out.println(">>>>>>update");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryInfoDto.setXgr(loginUser.getEmpUid());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,categoryInfoService.updateCategory(queryInfoDto));
    }
    //删除
    @PostMapping(value="/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult deleteCategory(@RequestBody CategoryInfoDto queryInfoDto){
        System.out.println(">>>>>>delete");
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,categoryInfoService.deleteCategory(queryInfoDto));
    }
    // 验证名称重复
    @PostMapping(value="/CategoryNameRules", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult isCategoryName(@RequestBody CategoryInfoDto queryInfoDto){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryInfoDto.setCjr(loginUser.getEmpUid());
        queryInfoDto.setFactoryId(loginUser.getFacCode());

        String categoryName = "";
        int count = categoryInfoService.isCategoryName(queryInfoDto);
        if(count > 0){
            categoryName = "名称已存在！";
        }
        return new AjaxResult(HttpStatus.SUCCESS, categoryName);
    }
    //动态下拉
    @PostMapping(value="/all",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult categoryAll(){
//        List<CategoryInfoDto> list = categoryInfoService.CategoryAll();
        LoginUser loginUser = SecurityUtils.getLoginUser();
//        demoVO.setFactoryId(loginUser.getFacCode());
        DemoVO build = DemoVO.builder().build();
        build.setFactoryId(loginUser.getFacCode());
        List<CategoryInfoDto> categoryInfoDtos = categoryInfoService.queryCategoryInfo(build);
        AjaxResult success = AjaxResult.success();
        success.put("data", categoryInfoDtos);
        return success;
    }
}
